package com.xumingxun.aibox.ai.impl;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.xumingxun.aibox.ai.AiConfigService;
import com.xumingxun.aibox.ai.AiService;
import com.xumingxun.aibox.domain.ai.AiModel;
import com.xumingxun.aibox.domain.ai.Message;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DeepseekAiServiceImpl implements AiService, AiConfigService {

    @Override
    public Map<String,String> loadConfig(String id) {
        Map<String,String> param=new HashMap<>();
        param.put("api_key","sk-60ec8161d399401984c1be8147c5b04b");
        param.put("api_url","https://api.deepseek.com/chat/completions");
        param.put("model","deepseek-chat");
        return param;
    }

    @Override
    public Message chat(AiModel aiModel,List<Message> messages) {
        Map<String,String> param = loadConfig(aiModel.getId());
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("model", param.get("model"));
        jsonObject.put("messages",messages);
        jsonObject.put("temperature", aiModel.getTemperature());
        jsonObject.put("max_tokens", aiModel.getMaxToken());
        jsonObject.put("stream",false);

        String result=HttpUtil.createPost(param.get("api_url")).header("Content-Type","application/json")
                .header("Authorization","Bearer "+param.get("api_key"))
                .body(jsonObject.toJSONString()).execute().body();


        System.out.println(result);
        JSONObject jsonResult = JSONObject.parseObject(result);
        JSONObject message= jsonResult.getJSONArray("choices").getJSONObject(0).getJSONObject("message");
        return new Message(message.getString("role"),message.getString("content") );
    }
}
